<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="商品名称" prop="commodityName">
        <el-input
          v-model="queryParams.commodityName"
          placeholder="请输入商品名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="所属商家" prop="businessId">
        <el-input
          v-model="queryParams.businessId"
          placeholder="请输入所属商家"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="商品分类" prop="commodityType">
        <el-select v-model="queryParams.commodityType" placeholder="请选择商品分类" clearable>
          <el-option
            v-for="dict in dict.type.commodity_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="已兑换数" prop="exchange">
        <el-input
          v-model="queryParams.exchange"
          placeholder="请输入已兑换数"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="兑换积分" prop="integral">
        <el-input
          v-model="queryParams.integral"
          placeholder="请输入兑换积分"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="商品库存" prop="inventory">
        <el-input
          v-model="queryParams.inventory"
          placeholder="请输入商品库存"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="商品状态" prop="commodityStatus">
        <el-select v-model="queryParams.commodityStatus" placeholder="请选择商品状态" clearable>
          <el-option
            v-for="dict in dict.type.commodity_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="是否推荐" prop="isRecommend">
        <el-select v-model="queryParams.isRecommend" placeholder="请选择是否推荐" clearable>
          <el-option
            v-for="dict in dict.type.sys_yes_no"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">
          搜索
        </el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['integral:commodity:add']"
        >
          新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['integral:commodity:edit']"
        >
          修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['integral:commodity:remove']"
        >
          删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['integral:commodity:export']"
        >
          导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="commodityList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="商品编号" align="center" prop="commodityId" />
      <el-table-column label="商品名称" align="center" prop="commodityName" />
      <el-table-column label="商品资料" align="center" prop="commodityFile" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.commodityFile" :width="50" :height="50" />
        </template>
      </el-table-column>
      <!-- <el-table-column label="所属商家" align="center" prop="businessName" /> -->
      <el-table-column label="商品分类" align="center" prop="commodityType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.commodity_type" :value="scope.row.commodityType" />
        </template>
      </el-table-column>
      <el-table-column label="已兑换数" align="center" prop="exchange" />
      <el-table-column label="兑换积分" align="center" prop="integral" />
      <el-table-column label="商品库存" align="center" prop="inventory" />
      <el-table-column label="商品状态" align="center" prop="commodityStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.commodity_status" :value="scope.row.commodityStatus" />
        </template>
      </el-table-column>
      <!-- <el-table-column label="商品描述" align="center" prop="remark" >
         <template slot-scope="scope">
         <div class="u-line-2">{{ scope.row.remark }}</div>
        </template>
      </el-table-column> -->
      <el-table-column label="是否推荐" align="center" prop="isRecommend">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_yes_no" :value="scope.row.isRecommend" />
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['integral:commodity:edit']"
          >
            修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['integral:commodity:remove']"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改商品信息对话框 -->
    <el-dialog
      :title="title"
      :visible.sync="open"
      width="700px"
      append-to-body
      :close-on-click-modal="false"
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="120px" class="flexForm">
        <el-form-item class="w50" label="商品名称" prop="commodityName">
          <el-input v-model="form.commodityName" placeholder="请输入商品名称" />
        </el-form-item>
        <el-form-item class="w50" label="是否推荐" prop="isRecommend">
          <el-select v-model="form.isRecommend" placeholder="请选择是否推荐">
            <el-option
              v-for="dict in dict.type.sys_yes_no"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <!-- <el-form-item class="w50" label="所属商家" prop="businessId">
          <el-select v-model="form.businessId" placeholder="请选择所属商家" filterable>
            <el-option
              v-for="item in businessList.rows"
              :key="item.businessId"
              :label="item.businessName"
              :value="item.businessId"
            ></el-option>
          </el-select>
        </el-form-item> -->
        <el-form-item class="w50" label="商品分类" prop="commodityType">
          <el-select v-model="form.commodityType" filterable placeholder="请选择商品分类">
            <el-option
              v-for="dict in dict.type.commodity_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="购买费用" prop="purchaseCost" class="w50">
          <el-input-number
            v-model="form.purchaseCost"
            controls-position="right"
            :min="1"
            :step="1"
            step-strictly
            placeholder="请输入购买费用"
          ></el-input-number>
        </el-form-item>
        <el-form-item class="w50" label="商品兑换码" prop="redeemCode">
          <el-input v-model="form.redeemCode" placeholder="请输入商品兑换码" />
        </el-form-item>
        <!-- <el-form-item class="w50" label="已兑换数" prop="exchange">
          <el-input v-model="form.exchange" placeholder="请输入已兑换数" />
        </el-form-item>
        <el-form-item class="w50" label="已兑换数" prop="exchange">
          <el-input v-model="form.exchange" placeholder="请输入已兑换数" />
        </el-form-item> -->
        <el-form-item class="w50" label="兑换积分" prop="integral">
          <el-input v-model="form.integral" placeholder="请输入兑换积分" />
        </el-form-item>
        <el-form-item class="w50" label="商品库存" prop="inventory">
          <el-input v-model="form.inventory" placeholder="请输入商品库存" />
        </el-form-item>
        <el-form-item class="w50" label="商品状态" prop="commodityStatus">
          <el-radio-group v-model="form.commodityStatus">
            <el-radio
              v-for="dict in dict.type.commodity_status"
              :key="dict.value"
              :label="dict.value"
            >
              {{ dict.label }}
            </el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item class="w50" label="购买地址" prop="purchaseAddress">
          <el-input v-model="form.purchaseAddress" placeholder="请输入购买地址" />
        </el-form-item>
        <el-form-item class="w50" label="购买联系人信息" prop="purchaseContact">
          <el-input
            v-model="form.purchaseContact"
            type="textarea"
            placeholder="请输入购买联系人信息"
          />
        </el-form-item>
        <el-form-item class="w50" label="商品资料" prop="commodityFile">
          <image-upload v-model="form.commodityFile" />
        </el-form-item>

        <el-form-item class="w50" label="商品描述" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listCommodity,
  getCommodity,
  delCommodity,
  addCommodity,
  updateCommodity
} from '@/api/integral/commodity'
import {
  listBusiness,
  getBusiness,
  delBusiness,
  addBusiness,
  updateBusiness
} from '@/api/integral/business'

export default {
  name: 'Commodity',
  dicts: ['commodity_type', 'sys_yes_no', 'commodity_status'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 商品信息表格数据
      commodityList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        commodityName: null,
        commodityFile: null,
        businessId: null,
        commodityType: null,
        exchange: null,
        integral: null,
        inventory: null,
        commodityStatus: null,
        isRecommend: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        commodityName: [{ required: true, message: '商品名称不能为空', trigger: 'blur' }],
        commodityFile: [{ required: true, message: '商品资料不能为空', trigger: 'blur' }],
        // businessId: [{ required: true, message: '所属商家不能为空', trigger: 'change' }],
        commodityType: [{ required: true, message: '商品分类不能为空', trigger: 'change' }],
        inventory: [{ required: true, message: '商品库存不能为空', trigger: 'blur' }],
        commodityStatus: [{ required: true, message: '商品状态不能为空', trigger: 'change' }],
        remark: [{ required: true, message: '商品描述不能为空', trigger: 'blur' }]
      },
      businessList: { rows: [], total: 0 }
    }
  },
  created() {
    this.getList()
    // this.getBusinessList()
  },
  methods: {
    /** 查询商家信息列表 */
    getBusinessList() {
      this.loading = true
      listBusiness().then((response) => {
        this.businessList.rows = response.rows
        this.businessList.total = response.total
        this.loading = false
      })
    },
    /** 查询商品信息列表 */
    getList() {
      this.loading = true
      listCommodity(this.queryParams).then((response) => {
        this.commodityList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        commodityId: null,
        commodityName: null,
        commodityFile: null,
        businessId: null,
        commodityType: null,
        exchange: null,
        integral: null,
        inventory: null,
        commodityStatus: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null,
        isRecommend: null
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.commodityId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = '添加商品信息'
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const commodityId = row.commodityId || this.ids
      getCommodity(commodityId).then((response) => {
        this.form = response.data
        this.open = true
        this.title = '修改商品信息'
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          if (this.form.commodityId != null) {
            updateCommodity(this.form).then((response) => {
              this.$modal.msgSuccess('修改成功')
              this.open = false
              this.getList()
            })
          } else {
            addCommodity(this.form).then((response) => {
              this.$modal.msgSuccess('新增成功')
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const commodityIds = row.commodityId || this.ids
      this.$modal
        .confirm('是否确认删除商品信息编号为"' + commodityIds + '"的数据项？')
        .then(function () {
          return delCommodity(commodityIds)
        })
        .then(() => {
          this.getList()
          this.$modal.msgSuccess('删除成功')
        })
        .catch(() => {})
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        'integral/commodity/export',
        {
          ...this.queryParams
        },
        `commodity_${new Date().getTime()}.xlsx`
      )
    }
  }
}
</script>
